﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySync2.Business.FileSystem;
using LibrarySync2.Business.FileSystem.Item;
using LibrarySync2.Common.Interfaces;

namespace LibrarySync2.ChildForms
{
    public partial class ManageBlacklistForm : Form
    {
        protected List<MediaFileFormListItem> ListItems = new List<MediaFileFormListItem>();
        protected List<IMediaFile> DifferenceList;

        public Blacklist Blacklist { get; protected set; }
        public Library PartnerLibrary { get; protected set; }
        public Library LocalLibrary { get; protected set; }


        public ManageBlacklistForm(Blacklist blacklist, Library partnerLibrary, Library localLibrary)
        {
            Blacklist = blacklist;
            PartnerLibrary = partnerLibrary;
            LocalLibrary = localLibrary;
            InitializeComponent();

            CreateListItems();
            BindListItems();
        }

        private void BindListItems()
        {
            gvBlacklist.DataSource = ListItems;
        }

        private void CreateListItems()
        {
            ListItems = new List<MediaFileFormListItem>();
            foreach (var blFile in Blacklist)
            {
                ListItems.Add(new MediaFileFormListItem(true, blFile));
            }

            if (PartnerLibrary == null)
                return;

            var libraryMediaFiles = PartnerLibrary.MediaFiles.FindAll(
                mf => Blacklist.FirstOrDefault<MediaFile>(bf => bf.Id == mf.Id) == null);

            foreach (var libraryFile in libraryMediaFiles)
            {
                ListItems.Add(new MediaFileFormListItem(false, libraryFile));
            }

            ListItems.Sort();
        }

        private void CreateExcludeLocalListItems()
        {
            ListItems = new List<MediaFileFormListItem>();
            foreach (var blFile in Blacklist)
            {
                ListItems.Add(new MediaFileFormListItem(true, blFile));
            }

            if (PartnerLibrary == null)
                return;

            if (DifferenceList == null)
            {
                if (LocalLibrary == null)
                    return;
                DifferenceList = LocalLibrary.FindDifferences(PartnerLibrary);
            }

            var libraryMediaFiles = DifferenceList.FindAll(
                mf => Blacklist.FirstOrDefault<MediaFile>(bf => bf.Id == mf.Id) == null);

            foreach (var libraryFile in libraryMediaFiles)
            {
                ListItems.Add(new MediaFileFormListItem(false, libraryFile));
            }

            ListItems.Sort();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var selectedFiles = ListItems.FindAll(mffli => mffli.IsSelected);
            var newBlacklist = new Blacklist();
            selectedFiles.ForEach(f => newBlacklist.Add(f.MediaFile as MediaFile));
            Blacklist = newBlacklist;

            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void cbDiff_CheckedChanged(object sender, EventArgs e)
        {
            
            if (cbDiff.Checked)
            {
                CreateExcludeLocalListItems();
                BindListItems();
            }
            else
            {
                CreateListItems();
                BindListItems();
            }
            
            gvBlacklist.Refresh();
        }
    }
}
